from activist.Data import Data
from activist.MetaData import MetaData

def commandCreate(name,address,phone):
    return Place(name,address,phone)

LOCALES = [('calgary.ab.ca','Calgary, Alberta, Canada')]

class Place(Data):
    def __init__(self,expectedid=None):
        super(Place,self).__init__(expectedid)
        self.command("create")
        
    command_create_VERSION = 0
    def command_create(self,name,address,phone,locale,notes,maplink):
        self._createcalled = True
        if type(phone) is not list:
            phone = [phone]

        self._name = name
        self._phone = phone
        self._address = address
        self._locale = locale
        self._notes = notes
        self._maplink = maplink

    def events(self):
        from activist.Event import Event
        from activist.EventInformation import EventInformation
        rtn = []
        for x in self.sourcesForRelationship(MetaData.LocatedAt):
            if isinstance(x,EventInformation) and not x.event() in rtn:
                if x.event() is None:
                    print "WARNING: I have event-information:",x,"but it has no event!"
                    raise "no event for " + str(x)
                    #rtn.append(x)
                else:
                    rtn.append(x.event())
        return rtn
    
    def __cmp__(self,other):
        return cmp(self._name.lower(),other._name.lower())

    def __str__(self):
        return "<Place %s>" % (self.id(),)

    def getXML(self):
        rtn = """<place class="activist.Place" id="%(id)s">
  <name>%(name)s</name>
  <address>%(address)s</address>
""" % { 'id':self.id(),
                'name':self._name,
                'address':self._address }

        for phone in self._phone:
            rtn = rtn + "  <phone>%s</phone>\n" % phone
        rtn = rtn + self.getMetaXML()
        return rtn + "\n</place>"


